#include <stdio.h>
#include <string.h>

main() {
	int number;
	int length;
	int a[100];
	char s[1000];
	int i;
	int up;
	int sum;
	int max;
	scanf("%d", &number);
	while (number--) {
		up = max = 0;
		sum = 0;
		memset(a, 0, sizeof(a));
		scanf("%s", &s);
		length = strlen(s);
		for (i = 0; i < length; i++) {

			if (s[i] != '(' && s[i] != ')' && s[i] != ',') {
				a[up]++;

			}
			if (s[i] == '(') {
				up++;
				max++;
			}
			if (s[i] == ')')
				up--;


		}

		for (i = 0; i <= max; i++)
			sum += a[i] * i;

		printf("%d\n", sum);


	}

}